package cn.iocoder.yudao.module.exam.controller.admin.examType;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.exam.controller.admin.examType.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.examType.ExamTypeDO;
import cn.iocoder.yudao.module.exam.service.examType.ExamTypeService;

@Tag(name = "管理后台 - 试题管理-题库类型")
@RestController
@RequestMapping("/exam/type")
@Validated
public class ExamTypeController {

    @Resource
    private ExamTypeService typeService;

    @PostMapping("/create")
    @Operation(summary = "创建试题管理-题库类型")
    @PreAuthorize("@ss.hasPermission('exam:type:create')")
    public CommonResult<Long> createType(@Valid @RequestBody ExamTypeSaveReqVO createReqVO) {
        return success(typeService.createType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新试题管理-题库类型")
    @PreAuthorize("@ss.hasPermission('exam:type:update')")
    public CommonResult<Boolean> updateType(@Valid @RequestBody ExamTypeSaveReqVO updateReqVO) {
        typeService.updateType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除试题管理-题库类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('exam:type:delete')")
    public CommonResult<Boolean> deleteType(@RequestParam("id") Long id) {
        typeService.deleteType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除试题管理-题库类型")
    @PreAuthorize("@ss.hasPermission('exam:type:delete')")
    public CommonResult<Boolean> deleteTypeList(@RequestParam("ids") List<Long> ids) {
        typeService.deleteTypeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得试题管理-题库类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('exam:type:query')")
    public CommonResult<ExamTypeRespVO> getType(@RequestParam("id") Long id) {
        ExamTypeDO type = typeService.getType(id);
        return success(BeanUtils.toBean(type, ExamTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得试题管理-题库类型分页")
    @PreAuthorize("@ss.hasPermission('exam:type:query')")
    public CommonResult<PageResult<ExamTypeRespVO>> getTypePage(@Valid ExamTypePageReqVO pageReqVO) {
        PageResult<ExamTypeDO> pageResult = typeService.getTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExamTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出试题管理-题库类型 Excel")
    @PreAuthorize("@ss.hasPermission('exam:type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTypeExcel(@Valid ExamTypePageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExamTypeDO> list = typeService.getTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "试题管理-题库类型.xls", "数据", ExamTypeRespVO.class,
                BeanUtils.toBean(list, ExamTypeRespVO.class));
    }

}